package com.sys.app.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sys.app.entity.page.PageQuery;
import com.sys.app.entity.result.PageResultDTO;
import com.sys.app.entity.sql.PageSql;
import com.sys.app.entity.sys.SysAccount;
import com.sys.app.entity.sys.SysMenu;
import com.sys.app.entity.util.StringUtil;
import com.sys.app.entity.util.TreeUtil;
import com.sys.app.service.page.IPageQueryService;
import com.sys.app.service.sys.ISysMenuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("sysMenu")
public class SysMenuContorller {

    private ISysMenuService iSysMenuService;
    private IPageQueryService iPageQueryService;
    public SysMenuContorller(ISysMenuService iSysMenuService,
                             IPageQueryService iPageQueryService){
        this.iSysMenuService =iSysMenuService;
        this.iPageQueryService=iPageQueryService;
    }
    /**
     * 主页首页
     * @return
     */
    @GetMapping("/index.do")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("main/sys/menu/index");
        return modelAndView;
    }

    /**
     * 新增/编辑
     * @return
     */
    @GetMapping("/edit.do")
    public ModelAndView  edit(Long id){
        ModelAndView modelAndView = new ModelAndView("main/sys/menu/edit");
        if(id!=null&&id.longValue()>0) {
            QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<SysMenu>();
            queryWrapper.eq("id", id);
            SysMenu entity = iSysMenuService.getOne(queryWrapper);
            modelAndView.addObject("entity", entity);
        }else{
            modelAndView.addObject("entity", new SysMenu());
        }
        return modelAndView;
    }

    @PostMapping("/getMenuList.do")
    @ResponseBody
    public PageResultDTO<SysMenu> getMenuList(PageQuery pageQuery) throws InstantiationException, IllegalAccessException {
        //IPage<Map<String,Object>> page1=iPageQueryService.pageList(pageQuery,PageSql.getPageData());
        IPage<SysMenu> page1=iPageQueryService.pageList(pageQuery,PageSql.getMenuList(),SysMenu.class);
        return new PageResultDTO().result(page1);
    }

    /**
     * 获取菜单表格数据
     * @param “state”:“closed, open“
     * @return
     */
    @PostMapping("/getMenuTreeList.do")
    @ResponseBody
    public List<SysMenu> getMenuTreeList() {

        QueryWrapper<SysMenu> queryWrapper=new QueryWrapper<SysMenu>();
        queryWrapper.eq("STATUS",0);
        queryWrapper.orderByAsc("MENU_ORDER");
        List<SysMenu> menuList=iSysMenuService.list(queryWrapper);

        List<SysMenu> tree=TreeUtil.buildTree(menuList);
       return tree;
    }



}
